package com.samon.guava.a1.Optional类;
/*
               _.---..._
           ./^         ^-._
         ./^C===.         ^\.   /\
        .|'     \\        _ ^|.^.|
   ___.--'_     ( )  .      ./ /||
  /.---^T\      ,     |     / /|||
 C'   ._`|  ._ /  __,-/    / /-,||
      \ \/    ;  /O  / _    |) )|,
       i \./^O\./_,-^/^    ,;-^,'
        \ |`--/ ..-^^      |_-^
         `|  \^-           /|:
          i.  .--         / '|.
           i   =='       /'  |\._
         _./`._        //    |.  ^-ooo.._
  _.oo../'  |  ^-.__./X/   . `|    | d
				 比克叔叔保佑
				 代码无BUG!
 */

import com.google.common.base.Optional;
import com.samon.guava.b1.P;

/**
 * @author WuZhongSheng on 2017/11/10
 */
public class GuavaTester {
	
	public static void main(String args[]){
		GuavaTester guavaTester = new GuavaTester();

		Integer value1 =  null;
		Integer value2 =  new Integer(10);
		// 允许空值: 使用Optional子类Absent, 与之对应的为Present
		Optional<Integer> a = Optional.fromNullable(value1);
		// 初始化, 如果为null, 会抛空指针
		Optional<Integer> b = Optional.of(value2);

		P.p(guavaTester.sum(a,b));
	}

	public Integer sum(Optional<Integer> a, Optional<Integer> b){
		// 检查是否存在. a对象属于Absent, 直接返回false
		P.p("First parameter is present: " + a.isPresent());

		P.p("Second parameter is present: " + b.isPresent());

		// 既然a是不存在的对象, 那它只能用默认值
		Integer value1 = a.or(new Integer(0));
		Integer value2 = b.get();

		return value1 + value2;
	}
}
